﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DongleTest
{
    public class ExportToCSV
    {
        /// <summary>
        /// 输出CSV的文件名
        /// </summary>
        private string fileName = string.Empty;

        /// <summary>
        /// 输出CSV的文件名
        /// </summary>
        public string FileName
        {
            set { fileName = value; }
            get {return fileName;}
        }

        public ExportToCSV()
        {
            string exeFileName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            string filePath = exeFileName.Substring(0, exeFileName.LastIndexOf('\\') + 1);
            string now = DateTime.Now.ToString("yyyyMMdd");

            if (!Directory.Exists(filePath +"Result\\"))
            {

                Directory.CreateDirectory(filePath +"Result\\");

            }
            fileName = filePath +"Result\\"+ now + ".csv";
        }

        public ExportToCSV(string fileName)
        {
            this.fileName = fileName;
        }

        /// <summary>
        /// 写CSV文件
        /// </summary>
        /// <param name="devInfo"></param>
        public void Write(DeviceInfo devInfo)
        {
            FileStream fs = null;
            bool isNew = false;
            if (System.IO.File.Exists(fileName))
            {
                fs = new FileStream(fileName, FileMode.Append);
            }
            else
            {
                fs = new FileStream(fileName, FileMode.CreateNew);
                isNew = true;
            }

            StreamWriter sw = new StreamWriter(fs);
            try
            {
                if (isNew)
                {
                    sw.WriteLine(HeadData(devInfo));
                }
                sw.WriteLine(RowData(devInfo));
                sw.Close();
                fs.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                if (isNew)
                {
                    System.IO.File.Delete(fileName);  
                }
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
        }

        private string RowData(DeviceInfo devInfo)
        {
            string data = string.Empty;
            
            System.Reflection.PropertyInfo[] properties = devInfo.GetType().GetProperties();
            string[] value=new string[properties.Length];
            for (int i = 0; i < properties.Length; i++)
            {
                value[i] = (string)properties[i].GetValue(devInfo,null);
            }
            data = string.Join(",", value);

            return data;
        }

        private string HeadData(DeviceInfo devInfo)
        {
            string data = string.Empty;

            System.Reflection.PropertyInfo[] properties = devInfo.GetType().GetProperties();
            string[] name = new string[properties.Length];
            for (int i = 0; i < properties.Length; i++)
            {
                name[i] = properties[i].Name;
            }
            data = string.Join(",", name);

            return data;
        }
    }
}
